package beuthTaskList;

import android.app.Activity;
import com.beuthTaskList.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.DatePicker;
import java.util.GregorianCalendar;

public class TaskCreateOrEdit extends Activity  {

	public DatePicker datePickerClosingDatePicker ;
	
	public EditText editTextTaskTimeSpendInputField ;
	
	public Spinner spinnerTaskTypeSelection ;
	
	public Button buttonSaveButton ;
	
	public Spinner spinnerTaskPrioritySelection ;
	
	public EditText editTextTaskNameInputField ;
	
	public TaskModel has = TaskModel.getInstance();
	
	public Button buttonDeleteButton ;
	
	public EditText editTextTaskPercentageStatusInputField ;
	

	//Start of user code protectedActivityAttributesTaskCreateOrEdit
	private TaskIO dataIO = new TaskIO();
	//End of user code	



	


 	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_taskcreateoredit);

			datePickerClosingDatePicker = (DatePicker) findViewById(R.id.datepicker_closingDatePicker);  
			editTextTaskTimeSpendInputField = (EditText) findViewById(R.id.edittext_taskTimeSpendInputField);  
			spinnerTaskTypeSelection = (Spinner) findViewById(R.id.spinner_taskTypeSelection);  
			buttonSaveButton = (Button) findViewById(R.id.button_saveButton);  
			spinnerTaskPrioritySelection = (Spinner) findViewById(R.id.spinner_taskPrioritySelection);  
			editTextTaskNameInputField = (EditText) findViewById(R.id.edittext_taskNameInputField);  
			buttonDeleteButton = (Button) findViewById(R.id.button_deleteButton);  
			editTextTaskPercentageStatusInputField = (EditText) findViewById(R.id.edittext_taskPercentageStatusInputField);  

			spinnerTaskTypeSelection.setAdapter(new ArrayAdapter<TaskType>(this, android.R.layout.simple_dropdown_item_1line, TaskType.values()));
			spinnerTaskPrioritySelection.setAdapter(new ArrayAdapter<Priority>(this, android.R.layout.simple_dropdown_item_1line, Priority.values()));


			buttonSaveButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//Start of user code protectedOnClickbuttonSaveButton
					if(has.getCurrentAdministration() == null) {
						Task task = new Task();
						task = fillTaskFromView(task);
						has.getAdministrates().add(task);	
					} else {
						fillTaskFromView(has.getCurrentAdministration());
					}
					has.setCurrentAdministration(null);
					dataIO.save(has.getAdministrates());
					Intent i = new Intent(v.getContext(), TaskList.class);
					startActivity(i);
					//End of user code
				}
			//Start of user code protectedOnClickListenerbuttonSaveButton
			//End of user code	
			});
			buttonDeleteButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//Start of user code protectedOnClickbuttonDeleteButton
					if(has.getCurrentAdministration() != null) {
						has.getAdministrates().remove(has.getCurrentAdministration());	
						has.setCurrentAdministration(null);
						dataIO.save(has.getAdministrates());
						Intent i = new Intent(v.getContext(), TaskList.class);
					   	startActivity(i);
					} else {
						Intent i = new Intent(v.getContext(), TaskCreateOrEdit.class);
					   	startActivity(i);
					}
					//End of user code
				}
			//Start of user code protectedOnClickListenerbuttonDeleteButton
			//End of user code	
			});
		//Start of user code protectedOnCreateTaskCreateOrEdit
		//End of user code
	}

 	@Override
    protected void onResume() {
    	super.onResume();
		//Start of user code protectedOnResumeTaskCreateOrEdit
        Bundle b = getIntent().getExtras();
        if(b != null && b.get("EditTaskNumber") != null) {
        	Task selectedTask = has.getAdministrates().get((Integer) b.get("EditTaskNumber"));
        	has.setCurrentAdministration(selectedTask);
        	//Werte setzen
        	editTextTaskNameInputField.setText(selectedTask.getName());
        	editTextTaskPercentageStatusInputField.setText("" + selectedTask.getStatusPercent());
        	editTextTaskTimeSpendInputField.setText("" + selectedTask.getTimeSpend());
        	spinnerTaskPrioritySelection.setSelection(selectedTask.getTaskPriority().ordinal());
        	spinnerTaskTypeSelection.setSelection(selectedTask.getTaskType().ordinal());
        	datePickerClosingDatePicker.updateDate(selectedTask.getClosingDate().get(GregorianCalendar.YEAR) ,selectedTask.getClosingDate().get(GregorianCalendar.MONTH) , selectedTask.getClosingDate().get(GregorianCalendar.DAY_OF_MONTH));
        }
		//End of user code
    }

 	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_beuthtasklist, menu);
        return true;
    }

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
			case R.id.m_taskCreateOrEdit:
				i = new Intent(this, TaskCreateOrEdit.class);
				startActivity(i);        			
				break;
			case R.id.m_taskList:
				i = new Intent(this, TaskList.class);
				startActivity(i);        			
				break;
      		default:
				break;
      	}
      return true;
    } 

	

	private Task fillTaskFromView(Task task) {
			
			task.setClosingDate(new GregorianCalendar(datePickerClosingDatePicker.getYear(), datePickerClosingDatePicker.getMonth(), datePickerClosingDatePicker.getDayOfMonth())); 
			if(!editTextTaskTimeSpendInputField.getText().toString().equals("")) {
			task.setTimeSpend(Integer.parseInt(editTextTaskTimeSpendInputField.getText().toString())); }
			
			task.setTaskType((TaskType) spinnerTaskTypeSelection.getSelectedItem()); 
			
			task.setTaskPriority((Priority) spinnerTaskPrioritySelection.getSelectedItem()); 
			
			task.setName(editTextTaskNameInputField.getText().toString()); 
			if(!editTextTaskPercentageStatusInputField.getText().toString().equals("")) {
			task.setStatusPercent(Integer.parseInt(editTextTaskPercentageStatusInputField.getText().toString())); }
		return task;
	}

//Start of user code protectedActivityCodeTaskCreateOrEdit
//End of user code

public TaskModel getHas() {
	return this.has;
}

public void setHas(TaskModel has) {
		this.has = has;
}	

} 
